<?php
namespace application\views;
use application\system\Gandalf;
class Template extends \application\auth\Normal {

	public function load() {
		global $concerto;
		// Include front-end functions
		require_once CN_APPLICATION_PATH.'application/functions/front-end.php';
		
		$queryVars = $concerto->queryVars;
		$template = parse_ini_file(templateDir().'info.ini');
		
		switch($queryVars['display']) {
			case 'content':
				Gandalf::generateToken();
				$concerto->ContentQuery->initMainQuery('limit='.$concerto->getOption('default_page_limit'));
				
				// Performed the normal content query, but which template file to include?
				if($queryVars['id']) {
					// Is there a specific template for that content?
					$permalink = DB()->getVar("SELECT `content_permalink` FROM `cn_content` WHERE `content_id`='".getQueryVar('id')."'");
					if(file_exists(templateDir() . $permalink . '.php')) {
						self::loadDisplay(templateDir() . $permalink . '.php');	
					}
					else {
						$type = DB()->getVar("SELECT `ct_title` FROM `cn_content_types` INNER JOIN `cn_content` ON `ct_id`=`content_type` WHERE `content_id`='".$queryVars['id']."'");
						$file = \application\utilities\Utility::makeUrl($type);
						self::loadDisplay(templateDir() . $file.'.php');
					}
				}
				elseif($queryVars['type']) {
					$file = \application\utilities\Utility::makeUrl($concerto->DB->getVar("SELECT `ct_plural` FROM `cn_content_types` WHERE `ct_id`='".$queryVars['type']."'"));
					self::loadDisplay(templateDir(). $file . '.php');
				}
				elseif($queryVars['search']) {
					self::loadDisplay(templateDir() . 'search.php');
				}
				else {
					self::loadDisplay(templateDir() . 'index.php');
				}
				break;
			case 'user':
				if(loggedIn()) {
					if(!$queryVars['id']) {
						setQueryVar('id', $concerto->User->getData('user_id'));
					}
					$concerto->UsersQuery->initMainQuery();
					// Display profile or dashboard
					if(!$queryVars['id'] || $queryVars['id'] == $concerto->User->getData('user_id')) {
						// Dashboard
						self::loadDisplay(templateDir().'dashboard.php');
					}
					else {
						self::loadDisplay(templateDir().'user.php');
					}
				}
				else {
					$currentUrl = $concerto->UrlInterpreter->currentUrl;
					header('location: '.CN_URL.'login/?alert=login_required&redirect='.str_replace('http://', '', $currentUrl));
				}
				break;
			default:
				if(getQueryVar('view'))
					self::loadDisplay(templateDir() . 'index.php');
				else
					self::loadDisplay(templateDir() . '404.php');
				break;
		}
		
	}
	protected static function loadDisplay($file) {
		global $concerto;
		// Add information to page views
		
		if(file_exists($file)) {
			$page = $concerto->UrlInterpreter->currentUrl;
			$insert = array('pv_page'=>$page, 'pv_ip'=>$_SERVER['REMOTE_ADDR'], 'pv_type'=>'normal');
			DB()->insert('cn_pageviews', $insert);
			Modulator()->runFunction('load_template_file', $file);
			ob_start();
			include $file;
			$html = ob_get_clean();
			$html = Modulator()->runFilter('html_output', $html);
			echo $html;
		}
		else {
			$concerto->Loader->loadError(ERROR_MISSING_TEMPLATE_FILE);
		}
		
		
		
	}
}
?>